package com.ckzp.core.util;

import com.ckzp.core.json.JsonUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 51594 on 2022/7/28.
 */
public class ParamUtil {

    /**
     * @param request
     * @param paramName
     * @return
     */
    public static String getString(HttpServletRequest request, String paramName) {
        String temp = request.getParameter(paramName);
        String temp1 = (String) request.getAttribute(paramName);
        if (temp != null && !"".equals(temp)) {
            return formatHttpString(temp);
        } else if (temp1 != null && !"".equals(temp1)) {
            return temp1;
        } else {
            return null;
        }
    }

    /**
     *
     * @param request
     * @param paramName
     * @return
     */
    public static String getBaseString(HttpServletRequest request, String paramName,
                                       String defaultString) {
        String temp = request.getParameter(paramName);
        if (temp == null) temp = "";
        return temp.length() == 0 ? defaultString : temp;
    }


    /**
     * @param request
     * @param paramName
     * @param defaultString
     * @return
     */
    public static String getString(HttpServletRequest request, String paramName, String defaultString) {
        String temp = getString(request, paramName);
        temp = com.ckzp.core.util.HtmlUtil.replaceParamHtml(temp);
        return temp.length() == 0 ? defaultString : temp;
    }

    /**
     * @param request
     * @param paramName
     * @param defaultInt
     * @return
     */
    public static int getInt(HttpServletRequest request, String paramName, int defaultInt) {
        try {
            String temp = getString(request, paramName);
            return temp == null ? defaultInt : Integer.parseInt(temp.trim());
        } catch (Exception e) {
            return defaultInt;
        }
    }


    public static String getRequestJson(HttpServletRequest request) {
        Map params = new HashMap();
        params.put("params", request.getParameterMap());
        params.put("AuthTtype", request.getAuthType());
        params.put("ContextPath", request.getContextPath());
        params.put("PathInfo", request.getPathInfo());
        params.put("PathTranslated", request.getPathTranslated());
        params.put("QueryString", request.getQueryString());
        params.put("RemoteUser", request.getRemoteUser());
        params.put("RequestURI", request.getRequestURI());
        params.put("RequestURL", request.getRequestURL());
        params.put("LocalAddr", request.getLocalAddr());
        params.put("RemoteAddr", request.getRemoteAddr());
        return JsonUtil.toJsonString(params);
    }

    private static String formatHttpString(String str) {
        if (str == null) return "";
        // str = str.replaceAll(" ", "");   //ȥ�ո�
        //str = str.replaceAll(" and ", "");//ȥ��and
        //str = str.replaceAll(" or ", ""); //ȥ��or
        //str = str.replaceAll("%20", ""); //ȥ�ո�
        // str = str.replaceAll("", "");
        str = str.replaceAll("&", "&amp;");
        return str;
    }


}
